package com.itcg.springboot03deptemptest.controller;

import com.itcg.springboot03deptemptest.pojo.*;
import com.itcg.springboot03deptemptest.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.OffsetDateTime;


@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @PutMapping
    public Result updates(@RequestBody Emp emp) {
        if (emp == null) {
            return Result.error("参数错误");
        }
        Emp empServiceById = empService.findById(emp.getId());
        if (empServiceById ==null) {
            return Result.success("没有此人");
        }
        empService.updates(emp);
        return Result.success();
    }

    @PostMapping
    public Result inserts(@RequestBody Emp emp) {
        try {
            if (emp == null) {
                return Result.error("参数错误");
            }
            int i = empService.inserts(emp);
            if (i > 0) {
                return Result.success(i);
            }
            return Result.error("添加失败");
        } catch (Exception e) {
            return Result.error("添加失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result deletes(@RequestBody Integer[] ids) {
        if (ids == null) {
            return Result.error("参数错误");
        }
        empService.deletes(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        if (id == null) {
            return Result.error("参数错误");
        }
        Emp emp = empService.findById(id);
        if (emp == null) {
            return Result.error("查无此人");
        }
        return Result.success(emp);
    }

    @GetMapping
    public Result findPage(EmpQuery empQuery) {
        PageBean<Emp> page = empService.findByCondition2(empQuery);
        return Result.success(page);
    }
}
















